package com.cskaoyan._04nio;

import org.junit.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * @description:
 * @author: 景天
 * @date: 2023/2/20 16:38
 **/
/*
RandomAccessFile特点

 */
public class RandomAccessFileTest {

    @Test
    public void constructorTest() throws IOException {
        System.out.println(System.getProperty("user.dir"));
        // 单元测试的相对路径
        // D:\workspace2\java48th\13-io
        // RandomAccessFile(String name, String mode)
        // 创建从中读取和向其中写入（可选）的随机访问文件流，该文件具有指定名称。
        // mode: r只读   rw读写
        RandomAccessFile raf =
                new RandomAccessFile("D:\\workspace2\\java48th\\a.txt", "rw");
        raf.write("abc".getBytes());

        raf.close();

    }

    @Test
    public void filePointerTest() throws IOException{
        RandomAccessFile raf =
                new RandomAccessFile("D:\\workspace2\\java48th\\a.txt", "rw");
        //getFilePointer() 获取当前文件指针
        long filePointer = raf.getFilePointer();
        System.out.println("filePointer = " + filePointer);
        int readData1 = raf.read();
        System.out.println(readData1);
        int readData2 = raf.read();
        System.out.println(readData2);

        int readData3 = raf.read();
        System.out.println(readData3);

        int readData4 = raf.read();
        System.out.println(readData4);

        System.out.println("raf.getFilePointer() = " + raf.getFilePointer());

        // seek(long pos)
        // 重置到此文件开头测量到的文件指针偏移量，
        // 在该位置发生下一个读取或写入操作。(将文件记录指针定位到pos位置)
        raf.seek(0);
        int readData5 = raf.read();
        System.out.println("readData5 = " + readData5);

        System.out.println("raf.getFilePointer() = " + raf.getFilePointer());


        raf.close();
    }

    @Test
    public void writeTest() throws IOException{
        RandomAccessFile raf =
                new RandomAccessFile("D:\\workspace2\\java48th\\a.txt", "rw");
        // write写数据
        // raf.write("abcdefg".getBytes());

        // 从文件的某个位置进行写数据
        // 移动指针
        raf.seek(4);
        raf.write("xxx".getBytes());
        raf.close();

    }

    // 从文件末尾开始写,实现追加
    @Test
    public void writeTest2() throws IOException{
        File file = new File("D:\\workspace2\\java48th\\a.txt");
        RandomAccessFile raf =
                new RandomAccessFile(file, "rw");
        // write写数据
        // 移动指针
        raf.seek(file.length());
        raf.write("efg".getBytes());

        raf.close();

    }

    @Test
    public void readTest() throws IOException{
        File file = new File("D:\\workspace2\\java48th\\a.txt");
        RandomAccessFile raf =
                new RandomAccessFile(file, "rw");
        // read

        // 从文件某个位置读取数据
        // 移动指针
        raf.seek(4);
        byte[] bytes = new byte[1024];
        int readCount = raf.read(bytes);
        System.out.println(new String(bytes,0,readCount));


        raf.close();

    }


    //向文件中指定位置插入数据
    @Test
    public void insertTest() throws IOException{
        // 创建对象
        File file = new File("D:\\workspace2\\java48th\\a.txt");
        RandomAccessFile raf =
                new RandomAccessFile(file, "rw");
        // 移动指针到4
        raf.seek(4);
        // 把指针后面的数据暂时保存一下
        StringBuffer sb = new StringBuffer();
        byte[] bytes = new byte[1024];
        int readCount;
        while ((readCount = raf.read(bytes)) != -1) {
            sb.append(new String(bytes, 0, readCount));
        }

        // 把指针移动到4
        raf.seek(4);

        // 直接写入新的数据
        raf.write("yyds".getBytes());

        // 把刚才保存的数据efg写到文件末尾
        raf.write(sb.toString().getBytes());
        // close
        raf.close();
    }
}
